package com.tianqu.tms.www.bid.action;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.www.bid.actionform.BidCarSerchAllForm;
import com.tianqu.tms.www.bid.controller.BidCarSerchAllListController;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchInputViewHelper;
/**
 * 高级搜索-车辆列表
 * @author mufeiyu
 *
 */
public class BidCarSerchAllListAction extends TmsAction {

    private Logger logger = Logger.getLogger(BidCarSerchAllListAction.class);
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		BidCarSerchAllForm carForm  = (BidCarSerchAllForm) form;
		BidCarSerchInputViewHelper view = new BidCarSerchInputViewHelper();
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, view.getPageNo());
			// 竞价开始时间
			uc.setAttribute(SORTKEY, "7");
			uc.setAttribute(MAXINDEX, view.getMaxIndex());
			// 倒序排列
			uc.setAttribute(ASC, "false");
			
			// 车辆搜素-查询输入画面迁移过来的场合serachFlg
			if ("1".equals(uc.getAttribute("serchFlg"))) {
				
				uc.removeAttribute("serchFlg");
				
				// 车辆搜索 - 查询输入 check
				if (StringUtil.isNullOrBlank(carForm.getLicensePlate())
						&& StringUtil.isNullOrBlank(carForm.getVin())
						&& StringUtil.isNullOrBlank(carForm.getStartTime())) {
					setErrorsToRequest(request, TmsConstants.U7003, null); // 请输入至少一个条件进行查询！
					return mapping.getInputForward();
				}
				
				if (!StringUtil.isNullOrBlank(carForm.getLicensePlate())
						&& carForm.getLicensePlate().length() != 5) {
					setErrorsToRequest(request, TmsConstants.IA010, null); // 请输入车牌号码后5位
					return mapping.getInputForward();
				} else {
					uc.setAttribute("searchLicensePlate",carForm.getLicensePlate());
				}
	
				if (!StringUtil.isNullOrBlank(carForm.getVin())
						&& carForm.getVin().length() != 6) {
					setErrorsToRequest(request, TmsConstants.IA011, null); // 请输入车架号后6位
					return mapping.getInputForward();
				} else {
					uc.setAttribute("searchVin",carForm.getVin());
				}
	
				if (!StringUtil.isNullOrBlank(carForm.getStartTime())) {
					String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
					Pattern p = Pattern.compile(eL);
					Matcher m = p.matcher(carForm.getStartTime());
					boolean dateFlag = m.matches();
					if (!dateFlag) {
						String[] param = {"竞价时间"};
						setErrorsToRequest(request, TmsConstants.U1014, param); // yyyy-MM-dd
						return mapping.getInputForward();
					}
					// 填写的日期
					SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
					Date inputDate = formatDate.parse(carForm.getStartTime());
					// 今天的日期
					Date currentDate = formatDate.parse(formatDate.format(new Date(System.currentTimeMillis())));
					// 比较
					if (inputDate.compareTo(currentDate) >= 0) {
						setErrorsToRequest(request, TmsConstants.U7004, null); // 竞价时间只能输入当天之前的日期（不包括当天日期）
						return mapping.getInputForward();
					} else {
						uc.setAttribute("searchStartTime",carForm.getStartTime());
					}
				}
			}
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}

		BidCarSerchAllListController controller = new BidCarSerchAllListController();
		controller.execute(uc);
		view = (BidCarSerchInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		
		//查询结果为空
		if(view.getList().size() == 0)
		{
			setErrorsToRequest(request, TmsConstants.U7006, null); // 没有符合条件的记录！
			return mapping.getInputForward();
		}
		
		AucCarInfo car = (AucCarInfo)uc.getAttribute(BidCarSerchAllListController.PAGING);
		// viewhelp中设置翻页。
		view.setPageNo(Integer.toString(car.getPageNo()));
		view.setMaxIndex(Integer.toString(car.getMaxIndex()));
		view.setAsc(Boolean.toString(car.isAsc()));
		view.setSortKey(Integer.toString(car.getSortKey()));
		view.setTotalCount(Integer.toString(car.getTotalCount()));
		view.setDateFrom(Integer.toString(car.getDateFrom()));
		view.setDateTo(Integer.toString(car.getDateTo()));
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	
	}
}
